// pages/choose/choose.js
const app = getApp();
var com = require("../../utils/common.js");
var chooseImgUrl = [];
var chooseScore = [];
var linkText = com.linkText;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mouthArr: "",
    filmArr: "",
    beShow: false,
    filmData: com.filmData,
    selectMonthIndex: 0,
    selectImgUrl: [],
    selectScore: [],
    movieNum: 1,
    scrollTop: ''
  },
  // 事件函数
  selectMonth: function (e) {  // 选择不同的月份
    var selectMonthIndex = e.currentTarget.dataset.index;
    this.setData({
      selectMonthIndex: selectMonthIndex,
      scrollTop: 0
    });
    // this.getMoive();
    
  },
  selectFilm: function (e) {

    // 选取电影
    var index = e.currentTarget.dataset.index;
    var filmData = this.data.filmData, mouthArr = this.data.mouthArr;
    var selectMonthIndex = this.data.selectMonthIndex;
    
    filmData[selectMonthIndex].filmList[index].active = !filmData[selectMonthIndex].filmList[index].active;

    this.data.filmData[this.data.selectMonthIndex].mouth.selectedLen = 0
    for (var i = 0; i < this.data.filmData[this.data.selectMonthIndex].filmList.length; i++) {
      if (filmData[selectMonthIndex].filmList[i].active){
        var movieNum = this.data.filmData[this.data.selectMonthIndex].mouth.selectedLen + 1;
        this.data.filmData[this.data.selectMonthIndex].mouth.selectedLen = movieNum;
      }
    }
    this.setData({
      movieNum: this.data.filmData[this.data.selectMonthIndex].mouth.selectedLen,
      filmData: this.data.filmData
    });
    console.log(this.data.filmData[this.data.selectMonthIndex].mouth.selectedLen);
    
    // 显示每月选择了几部电影
    var filmName = e.currentTarget.dataset.name;
    var imgUrl = e.currentTarget.dataset.url;
    var scoreVal = e.currentTarget.dataset.score + "|" + filmName;
    if (!(filmData[selectMonthIndex].filmList[index].name.indexOf(filmName) === -1)) {
      filmData[selectMonthIndex].mouth.active = true;
    }else{
      filmData[selectMonthIndex].mouth.active = false;
    }
    this.setData({
      filmData: filmData
    });

    // 将选择的电影保存
    var imgUrlIndexOf = this.data.selectImgUrl.indexOf(imgUrl);
    var scoreValIndexOf = this.data.selectScore.indexOf(scoreVal);
    // 电影海报
    if (imgUrlIndexOf === -1) {
      this.data.selectImgUrl.push(imgUrl);
    } else {
      this.data.selectImgUrl.splice(imgUrlIndexOf, 1);
    }
    console.log(this.data.selectImgUrl)
    // 电影评分
    if (scoreValIndexOf === -1) {
      this.data.selectScore.push(scoreVal);
    } else {
      this.data.selectScore.splice(scoreValIndexOf, 1);
    }
    this.setData({
      selectImgUrl: this.data.selectImgUrl,
      selectScore: this.data.selectScore
    });
    chooseImgUrl = this.data.selectImgUrl;
    chooseScore = this.data.selectScore;
  },
  toResult: function(){
    wx.navigateTo({
      url: '../result/result?chooseImgUrl=' + chooseImgUrl + "&chooseScore=" + chooseScore
    });
  },

  // 电影列表
  getMoive() {
    wx.request({
      url: "https://yhw666.mynatapp.cc/man/urlList",
      data: {
        pageNo: 1,
        pageSize: 10,
        type: this.data.selectMonthIndex + 1
      }, 
      success: (res) => {
        console.log(res)
        this.setData({
          filmData: res.data.urls.list
        });
         
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    // this.getMoive();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var random = parseInt(Math.random() * 3 + 1);
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: linkText[random],
      imageUrl: '../../images/forward.png',
      path: '/pages/index/index',
      success: function (e) {
        var shareTickets = res.shareTickets;
        if (shareTickets.length == 0) {
          return false;
        }
        wx.getShareInfo({
          shareTicket: shareTickets[0],
          success: function (res) {
            var encryptedData = res.encryptedData;
            var iv = res.iv;
          }
        });
        wx.showShareMenu({
          withShareTicket: true
        });
      },

    }
  }
})